package com.wjerp.tplus.server.api.controller;

import com.wjerp.tplus.server.api.controller.base.BaseController;
import com.wjerp.tplus.server.application.biz.AuthenticationBiz;
import com.wjerp.tplus.server.common.exception.BusinessException;
import com.wjerp.tplus.server.common.exception.ServiceException;
import com.wjerp.tplus.server.common.model.BaseResponseData;
import com.wjerp.tplus.server.common.model.LoginResponse;
import com.wjerp.tplus.server.common.model.LoginWrapper;
import com.wjerp.tplus.server.common.utils.RSAUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author lishuailei
 */
@Api(tags = "登录相关api")
@RestController
@RequestMapping(value="auth")
public class AuthenticationController extends BaseController {

	@Resource
	private AuthenticationBiz authenticationBiz;

	@ApiOperation(value = "登录接口")
	@RequestMapping(value="login", method= RequestMethod.POST)
	public ResponseEntity<BaseResponseData> login(@ApiParam(value="登录所需参数体") @RequestBody LoginWrapper loginWrapper, HttpServletRequest request) throws AuthenticationException {

		LoginResponse response =  null;
		try{
			response = authenticationBiz.loginPlatform(request, loginWrapper);
		}catch(BusinessException e){
			return fail(e.getErrorCode(), e.getErrorMsg(), e.getCause());
		}
		return success(response);
	}

	@ApiOperation(value = "获取 RSA public key")
	@RequestMapping(value="getPublicKey", method= RequestMethod.GET)
	public ResponseEntity<BaseResponseData> getPublicKey(){
		try {
			String publicKey = RSAUtils.getPublicKey();
			return success(publicKey);
		} catch (Exception e) {
			e.printStackTrace();
			return fail(null, "获取 RSA public key 失败", e.getCause());
		}
	}

	@ApiOperation(value = "生成密钥对")
	@RequestMapping(value="generateKeyPair", method= RequestMethod.GET)
	public ResponseEntity<BaseResponseData> generateKeyPair(){
		try {
			RSAUtils.generateKeyPair();
			return success(true);
		} catch (Exception e) {
			e.printStackTrace();
			return fail(null, "生成密钥对失败", e.getCause());
		}
	}

	@ApiOperation(value = "生成指定的加密密码")
	@RequestMapping(value="generatePassword/{password}", method= RequestMethod.GET)
	public ResponseEntity<BaseResponseData> generatePassword(@PathVariable(value = "password") String password){
		try {
			password =  RSAUtils.encryptBASE64(RSAUtils.encryptByPublicKey(password));
			return success(password);
		} catch (Exception e) {
			e.printStackTrace();
			return fail(null, "生成指定的加密密码失败", e.getCause());
		}
	}

	@ApiOperation("系统注册")
	@PutMapping("sysRegister/{authCode}")
	public ResponseEntity<BaseResponseData> sysRegister(@PathVariable(value = "authCode") String authCode){
		try {
			authenticationBiz.sysRegister(authCode);
			return success(true);
		} catch (ServiceException e) {
			e.printStackTrace();
			return fail(e.getErrorCode(),e.getErrorMsg(),e);
		} catch (BusinessException e) {
			e.printStackTrace();
			return fail(e.getErrorCode(),e.getErrorMsg(),e);
		}
	}
}
